<template>
  <div>
    <div class="top-banner">
      <div class="container">
        <div class="top">
          <div>欢迎来到我的网上商店！</div>
          <div v-if="!isLogin">
            <span style="cursor: pointer" @click="$router.push('/login')">登录或注册</span>
          </div>
          <div v-if="isLogin" class="top_user">
            <el-avatar :size="30" :src="userInfo?.icon" >
              <img :src="userInfo?.icon"/>
            </el-avatar>
            <el-dropdown trigger="hover" @command="handleCommand">
              <span class="nickname" style="cursor: pointer">
                {{ userInfo.nickname }}
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="points">我的积分</el-dropdown-item>
                <el-dropdown-item command="balance">我的余额</el-dropdown-item>
                <el-dropdown-item command="address">我的地址</el-dropdown-item>
                <el-dropdown-item command="orders">我的订单</el-dropdown-item>
                <el-dropdown-item command="cart">我的购物车</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="header">
        <div class="logo" style="cursor: pointer" @click="$router.push('/')">
          <img src="https://order-ly.oss-cn-guangzhou.aliyuncs.com/logo.png" style="height: 60px"/>
        </div>

        <div class="search-bar">
          <input type="text" class="search-input" v-model="searchText" placeholder="请输入商品名称"/>
          <button class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </button>
        </div>

        <div class="user-actions">
          <div class="cart" @click="goto('cart')">
            <i class="el-icon-shopping-cart-2" style="font-size: 26px"></i>
            <span class="cart-count">{{ cartCount }}</span>
          </div>
          <div class="account" @click="goto('userInfo')">
            <i class="el-icon-user" style="font-size: 26px"></i>
          </div>
        </div>
      </div>
    </div>
    <Navigation></Navigation>
  </div>
</template>

<script>
import Navigation from "./home/Navigation";

export default {
  name: "Header",
  components: {
    Navigation,
  },

  data() {
    return {
      searchText: "",
      cartCount: 0,
    };
  },
  computed: {
    isLogin() {
      return this.$store.getters.isLogin ;
    },
    userInfo() {
      return this.$store.getters.userInfo ;
    }
  },
  methods: {
    search() {
      this.$router.push({path: "/allPro", query: {keyword:"search", text:this.searchText }})
    },
    handleCommand(command) {
      switch (command) {
        case "points":
            this.$router.push("/UserInfo/Integration")
          break;
        case "balance":
          this.$router.push("/UserInfo/balance")
          break;
        case "address":
          this.$router.push("/UserInfo/address")
          break;
        case "orders":
          this.$router.push("/UserInfo/order")
          break;
        case "cart":
          this.$router.push("/cart");
          break;
        case "logout":
          this.userInfo = null
          this.isLogin = false
          localStorage.removeItem("TOKEN");
          this.$store.dispatch("logout")
          this.$message.success("已退出登录");
          this.$router.push("/");
          break;
        default:
          console.log("未知操作:", command);
      }
    },
    goto(item){
      if(item=="userInfo"){
        this.$router.push({path:`/UserInfo/Integration`})
      }else {
        this.$router.push({
          path: "/myCart",
        });
      }
    }
  },
};
</script>

<style scoped lang="less">
.top-banner {
  background-color: #8ab660;
  color: white;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
}

.search-bar {
  display: flex;
  align-items: center;
  flex-grow: 1;
  max-width: 600px;
  margin: 0 40px;
  position: relative;
}

.search-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e0e0e0;
  border-radius: 4px 0 0 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.top_user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nickname {
  font-size: 16px;
  font-weight: bold;
  color: white;
}

.search-input:focus {
  outline: none;
  border-color: #8ab660;
}

.search-button {
  padding: 0 20px;
  height: 51px;
  background-color: #8ab660;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
}

.search-button:hover {
  background-color: #7aa550;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

.cart {
  position: relative;
  cursor: pointer;
}

.cart-count {
  height: 12px;
  width: 10px;
  line-height: 13px;
  position: absolute;
  top: -5px;
  right: -16px;
  background-color: #8ab660;
  color: white;
  padding: 2px 6px;
  border-radius: 50%;
  font-size: 12px;
}

.account {
  cursor: pointer;
}

@media (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 15px;
  }

  .search-bar {
    margin: 15px 0;
    width: 100%;
  }

  .user-actions {
    width: 100%;
    justify-content: space-around;
  }
}
</style>